﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Diagnostics;

namespace WpfApplication2
{
    /// <summary>
    /// Interaction logic for ConnectionWindow.xaml
    /// </summary>
    public partial class ConnectionWindow : Window
    {
        private ServiceController.ServiceController serviceController;
        public ConnectionWindow(ServiceController.ServiceController _serviceController)
        {
            InitializeComponent();
            serviceController = _serviceController;
            this.Loaded += new RoutedEventHandler(ConnectionWindow_Loaded);
        }

        void ConnectionWindow_Loaded(object sender, RoutedEventArgs e)
        {
            TextBoxSubscriptionID.Focus();
        }

        private void ButtonBrowse_Click(object sender, RoutedEventArgs e)
        {


            OpenFileDialog OpenFiledialog = new OpenFileDialog();
            OpenFiledialog.Title = "Select a certification file";
            OpenFiledialog.Filter = "Certificate files (*.cer)|*.cer|All files (*.*)|*.*";

            if ((bool)OpenFiledialog.ShowDialog())
            {
                TextBoxCertificatePath.Text = OpenFiledialog.FileName.ToString();
            }


            
  


        }

        

       

        private void ButtonConnect_Click(object sender, RoutedEventArgs e)
        {
            if (CheckValidation())
            {

                App.SubscriptionID = TextBoxSubscriptionID.Text;
                App.CertificatePath = TextBoxCertificatePath.Text;
                serviceController.GetListHostServicesAsync(TextBoxSubscriptionID.Text, TextBoxCertificatePath.Text, null);
                //serviceController.GetStorageAccountListAsync(TextBoxSubscriptionID.Text, TextBoxCertificatePath.Text);

                this.DialogResult = true;
                this.Close();
            }
        }

        private bool CheckValidation()
        {
            if (String.IsNullOrEmpty(TextBoxSubscriptionID.Text))
            {
                MessageBox.Show("Subscription ID cannot be empty.","Error",MessageBoxButton.OK,MessageBoxImage.Error);
                TextBoxSubscriptionID.Focus();
                return false;
            }
            if (String.IsNullOrEmpty(TextBoxCertificatePath.Text))
            {
                MessageBox.Show("Certificate location cannot be empty.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                TextBoxCertificatePath.Focus();
                return false;
            }
            return true;
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void HandleRequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            string navigateUri = hl.NavigateUri.ToString();
            Process.Start(new ProcessStartInfo(navigateUri));
            e.Handled = true; 
        }
        
    }
}
